var chargingRecordDataLeft = [];
var chargingRecordDataRight = [];
var index = undefined;
var dataFlag = 1;
var chargingChart = undefined;
var outputVoltageData = [];
var outputElectricData = [];
var currentSOCData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	selectDate("startDate");
	selectDate("endDate");
	setTimeout(hideLoading, 500);
});

function mores(){
	dataFlag = dataFlag + 1;
	if((dataFlag - 1) * 3 < chargingRecordDataLeft.length){
		searchLeft();
	}else{
		$(".more").html('');
		$(".more").html('已加载全部');
	}
}
function search(){
	dataFlag = 1;
	$("#curves").empty();
	dateList = [];
	var carNumber = $("#carNumber").val();
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if(!carNumber){
		$("#carNumber").focus();
		$("#carNumber").tips({
			side : 2,
			msg : "请填写车辆自标号",
			bg : '#AE81FF',
			time : 1
		});
	} else if (!startChargingTime){
		$("#startChargingTime").focus();
		$("#startChargingTime").tips({
			side : 2,
			msg : "请选择开始时间",
			bg : '#AE81FF',
			time : 1
		});
	} else if (!endChargingTime){
		$("#endChargingTime").focus();
		$("#endChargingTime").tips({
			side : 2,
			msg : "请选择结束时间",
			bg : '#AE81FF',
			time : 1
		});
	} else if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("时间1不能大于时间2！");
	} else {
		$ajax('searchChargingRecord', {CarNumber: carNumber, StartChargingTime: startChargingTime}, searchChargingRecordLeft);
		$ajax('searchChargingRecord', {CarNumber: carNumber, StartChargingTime: endChargingTime}, searchChargingRecordRight);
	}
}
function searchChargingRecordLeft(data){
	index = 0;
	chargingRecordDataLeft = data && data.chargingRecordList;
	searchLeft();
}
function searchChargingRecordRight(data){
	index = 0;
	chargingRecordDataRight = data && data.chargingRecordList;
}
function searchLeft(){
	if(chargingRecordDataLeft[index] && index < dataFlag * 3){
		var div = $("#curves");
		$('<div class="col-sm-6" id="chartLeft'+index+'" style="height:300px;">').appendTo(div);
		chargingChart = echarts.init(document.getElementById('chartLeft' + index));
		chargingChart.showLoading({text : "功率曲线加载中..."});
		$ajax('chargingChartHistory', 
				  {EquipmentNo: chargingRecordDataLeft[index].equipmentNo,
				   CardNo: chargingRecordDataLeft[index].cardNo,
				   CarNumber: chargingRecordDataLeft[index].carNumber,
				   StartChargingDate: timeStamp2String(chargingRecordDataLeft[index].startChargingDTTM),
				   EndChargingDate: timeStamp2String(chargingRecordDataLeft[index].endChargingDTTM)
				  }, 
				  searchChargingChartLeft, '', searchChargingChartLeft);
	}
}

function searchChargingChartLeft(data){
	if(data && data.outputVoltage && data.outputVoltage.length > 0){
		outputVoltageData = data.outputVoltage || [];
		outputElectricData = data.outputElectric || [];
		currentSOCData = data.currentSOC || [];
		setTimeout(function (){chargingChart.hideLoading();}, 200);
		changeEchart();
	} else {
		setTimeout(function (){chargingChart.hideLoading();}, 200);
	}
	setTimeout(searchRight, 200);
}
function searchRight(){
	if(chargingRecordDataRight[index]){
		var div = $("#curves");
		$('<div class="col-sm-6" id="chartRight'+index+'" style="height:300px;">').appendTo(div);
		chargingChart = echarts.init(document.getElementById('chartRight' + index));
		chargingChart.showLoading({text : "功率曲线加载中..."});
		$ajax('chargingChartHistory', 
				  {EquipmentNo: chargingRecordDataRight[index].equipmentNo,
				   CardNo: chargingRecordDataRight[index].cardNo,
				   CarNumber: chargingRecordDataRight[index].carNumber,
				   StartChargingDate: timeStamp2String(chargingRecordDataRight[index].startChargingDTTM),
				   EndChargingDate: timeStamp2String(chargingRecordDataRight[index].endChargingDTTM)
				  }, 
				  searchChargingChartRight, '', searchChargingChartRight);
	} else {
		var div = $("#curves");
		$('<div class="col-sm-6" style="height:300px;">').appendTo(div);
		index = index + 1;
		searchLeft();
	}
}

function searchChargingChartRight(data){
	if(data && data.outputVoltage && data.outputVoltage.length > 0){
		outputVoltageData = data.outputVoltage || [];
		outputElectricData = data.outputElectric || [];
		currentSOCData = data.currentSOC || [];
		setTimeout(function (){chargingChart.hideLoading();}, 200);
		changeEchart();
	} else {
		setTimeout(function (){chargingChart.hideLoading();}, 200);
	}
	index = index + 1;
	setTimeout(searchLeft, 200);
}

function changeEchart(){
    var option = {
            tooltip : {
            	trigger: 'axis',
            	axisPointer:{
            		label: {
            			formatter: function (params) {
            				if (params && params.value) {
            					return timeStamp2String(new Date(params.value)).substr(11);
            				} else {
            					'时间异常';
            				}
            			}
            		}
            	}
            },
            legend: {
            	x: 'right',
                data:['充电电压','充电电流','充电SOC']
            },
            xAxis: [
                {
                	type: 'time',
                	name: '充电时间',
                	min: (outputVoltageData && outputVoltageData[0] && outputVoltageData[0][0]) || '',
                	max: (outputVoltageData && outputVoltageData[outputVoltageData.length - 1] && outputVoltageData[outputVoltageData.length - 1][0]) || '',
                	splitNumber: (outputVoltageData && outputVoltageData.length && outputVoltageData.length) < 10 ? outputVoltageData && outputVoltageData.length : 10,
                	axisLabel: {
                		formatter : function (chargingTime) {
                			if(chargingTime){
                				return timeStamp2String(new Date(chargingTime)).substr(11);
                			} else {
                				return '';
                			}
                        }
                	}
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '充电电压',
                	min: 0,
                	max: 750,
                    position: 'left',
                    axisLine: {
                        lineStyle: {
                            color: '#01b2ac',
                        }
                    },
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                {
                    type: 'value',
                    name: '充电电流',
                    min: 0,
                    max: 750,
                    position: 'right',
                    axisLine: {
                        lineStyle: {
                            color: '#09be09',
                        }
                    },
                    axisLabel: {
                        formatter: '{value}'
                    }
                },
                {
                    type: 'value',
                    name: '充电SOC',
                    min: 0,
                    max: 100,
                    position: 'right',
                    offset: 50,
                    axisLine: {
                        lineStyle: {
                            color: '#fe902f',
                        }
                    },
                    axisLabel: {
                        formatter: '{value}'
                    }
                }
            ],
            series: [
                {
                    name:'充电电压',
                    type:'line',
                    symbol:'none',
                    itemStyle: {
                        normal : {
                            color: '#01b2ac',
                        }
                    },
                    data:outputVoltageData
                },
                {
                    name:'充电电流',
                    type:'line',
                    symbol:'none',
                    yAxisIndex:1,
                    itemStyle: {
                        normal : {
                            color: '#09be09',
                        }
                    },
                    data:outputElectricData
                },
                {
                    name:'充电SOC',
                    type:'line',
                    symbol:'none',
                    yAxisIndex:2,
                    itemStyle: {
                        normal : {
                            color: '#fe902f',
                        }
                    },
                    data:currentSOCData
                }
            ]
        };
    chargingChart.setOption(option);
}